package com.atguigu.lease.web.admin.service.impl;

import com.atguigu.lease.model.entity.*;
import com.atguigu.lease.model.enums.ItemType;
import com.atguigu.lease.web.admin.mapper.ApartmentInfoMapper;
import com.atguigu.lease.web.admin.service.*;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentDetailVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentItemVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentQueryVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentSubmitVo;
import com.atguigu.lease.web.admin.vo.graph.GraphVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author liubo
 * @description 针对表【apartment_info(公寓信息表)】的数据库操作Service实现
 * @createDate 2023-07-24 15:48:00
 */
@Service
public class ApartmentInfoServiceImpl extends ServiceImpl<ApartmentInfoMapper, ApartmentInfo>
        implements ApartmentInfoService {

    @Autowired
    private GraphInfoService graphInfoService;
    @Autowired
    private ApartmentFacilityService facilityService;
    @Autowired
    private ApartmentLabelService labelService;
    @Autowired
    private ApartmentFeeValueService feeValueService;
    @Autowired
    private ProvinceInfoService provinceInfoService;
    @Autowired
    private CityInfoService cityInfoService;
    @Autowired
    private  DistrictInfoService districtInfoService;
    @Autowired
    private ApartmentInfoMapper apartmentInfoMapper;

    @Override
    public void saveOrUpdateApartment(ApartmentSubmitVo apartmentSubmitVo) {
        //检查是插入还是修改
        if(apartmentSubmitVo.getId() != null){
            //获取公寓id
            Long id = apartmentSubmitVo.getId();
            //1.删除图片
            LambdaQueryWrapper<GraphInfo>  graphInfoQuery = new LambdaQueryWrapper<>();
            graphInfoQuery.eq(GraphInfo::getItemId, id)
                    .eq(GraphInfo::getItemType, ItemType.APARTMENT);
            graphInfoService.remove(graphInfoQuery);

            //2.删除配套
            LambdaQueryWrapper<ApartmentFacility> facilityInfoQuery = new LambdaQueryWrapper<>();
            facilityInfoQuery.eq(ApartmentFacility::getApartmentId, id);
            facilityService.remove(facilityInfoQuery);

            //3.删除标签
            LambdaQueryWrapper<ApartmentLabel> labelQuery = new LambdaQueryWrapper<>();
            labelQuery.eq(ApartmentLabel::getApartmentId,id);
            labelService.remove(labelQuery);

            //4.删除杂费
            LambdaQueryWrapper<ApartmentFeeValue> feeValueQuery = new LambdaQueryWrapper<>();
            feeValueQuery.eq(ApartmentFeeValue::getApartmentId,id);
            feeValueService.remove(feeValueQuery);

        }
        //补充：前端未传送省市县名称只有id
        ProvinceInfo provinceInfo = provinceInfoService.getById(apartmentSubmitVo.getProvinceId());
        CityInfo cityInfo = cityInfoService.getById(apartmentSubmitVo.getCityId());
        DistrictInfo districtInfo = districtInfoService.getById(apartmentSubmitVo.getDistrictId());
        apartmentSubmitVo.setProvinceName(provinceInfo.getName());
        apartmentSubmitVo.setCityName(cityInfo.getName());
        apartmentSubmitVo.setDistrictName(districtInfo.getName());

        //公寓入库回显
        this.saveOrUpdate(apartmentSubmitVo);
        Long apartmentId = apartmentSubmitVo.getId();



        //1.新增图图片
        List<GraphVo> graphVoList = apartmentSubmitVo.getGraphVoList();
        if (!CollectionUtils.isEmpty(graphVoList)){
            List<GraphInfo> graphInfoList = new ArrayList<>(graphVoList.size());
            graphVoList.forEach(graphVo -> {
                GraphInfo info = new GraphInfo();
                info.setName(graphVo.getName());
                info.setUrl(graphVo.getUrl());
                info.setItemType(ItemType.APARTMENT);
                //入库公寓iD
                info.setItemId(apartmentId);
                graphInfoList.add(info);
            });
            graphInfoService.saveBatch(graphInfoList);
        }
        //2.新增配套
        List<Long> facilityInfoIds = apartmentSubmitVo.getFacilityInfoIds();
        List<ApartmentFacility> apartmentFacilityList = new ArrayList<>(facilityInfoIds.size());
        if (!CollectionUtils.isEmpty(facilityInfoIds)){
            facilityInfoIds.forEach(facilityInfoId ->{
                ApartmentFacility info = ApartmentFacility.builder().facilityId(facilityInfoId).apartmentId(apartmentId).build();

                apartmentFacilityList.add(info);
            });
            facilityService.saveBatch(apartmentFacilityList);
        }

        //3.新增标签
        List<Long> labelIds = apartmentSubmitVo.getLabelIds();
        List<ApartmentLabel> labelList = new ArrayList<>(labelIds.size());
        if (!CollectionUtils.isEmpty(labelIds)){
            labelIds.forEach(labelId ->{
                ApartmentLabel info = ApartmentLabel.builder().labelId(labelId).apartmentId(apartmentId).build();
                labelList.add(info);
            });
            labelService.saveBatch(labelList);
        }

        //4.新增杂费
        List<Long> feeValueIds = apartmentSubmitVo.getFeeValueIds();
        List<ApartmentFeeValue> feeValueList = new ArrayList<>(feeValueIds.size());
        feeValueIds.forEach(feeValueId ->{
            ApartmentFeeValue info = ApartmentFeeValue.builder().feeValueId(feeValueId).apartmentId(apartmentId).build();
            feeValueList.add(info);
        });
        feeValueService.saveBatch(feeValueList);


    }

    @Override
    public IPage<ApartmentItemVo> pageItem(IPage<ApartmentItemVo> page, ApartmentQueryVo queryVo) {
        page = apartmentInfoMapper.pageItem(page,queryVo);
        return page;
    }

    @Override
    public ApartmentDetailVo getDetailById(Long id) {

        return apartmentInfoMapper.getDetailById(id);
    }

    @Override
    public void removeApartmentById(Long id) {
        //1.删除GraphInfo
        LambdaQueryWrapper<GraphInfo> graphInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
        graphInfoLambdaQueryWrapper.eq(GraphInfo::getItemId, id)
                                   .eq(GraphInfo::getItemType,1);
        graphInfoService.remove(graphInfoLambdaQueryWrapper);

        //2.删除ApartmentLabel
        LambdaQueryWrapper<ApartmentLabel> apartmentLabelLambdaQueryWrapper = new LambdaQueryWrapper<>();
        apartmentLabelLambdaQueryWrapper.eq(ApartmentLabel::getApartmentId, id);
        labelService.remove(apartmentLabelLambdaQueryWrapper);

        //3.删除ApartmentFacility
        LambdaQueryWrapper<ApartmentFacility> apartmentFacilityLambdaQueryWrapper = new LambdaQueryWrapper<>();
        apartmentFacilityLambdaQueryWrapper.eq(ApartmentFacility::getApartmentId,id );
        facilityService.remove(apartmentFacilityLambdaQueryWrapper);

        //4.删除ApartmentFeeValue
        LambdaQueryWrapper<ApartmentFeeValue> apartmentFeeValueLambdaQueryWrapper = new LambdaQueryWrapper<>();
        apartmentFeeValueLambdaQueryWrapper.eq(ApartmentFeeValue::getApartmentId, id);
        feeValueService.remove(apartmentFeeValueLambdaQueryWrapper);

        //5.删除公寓
        this.removeById(id);
    }

}




